package com.xmyx.toponad;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.widget.Toast;

import androidx.annotation.NonNull;

import com.anythink.core.api.ATAdInfo;
import com.anythink.core.api.ATSDK;
import com.anythink.core.api.AdError;
import com.anythink.rewardvideo.api.ATRewardVideoAd;
import com.anythink.rewardvideo.api.ATRewardVideoListener;

import org.godotengine.godot.Godot;
import org.godotengine.godot.plugin.GodotPlugin;
import org.godotengine.godot.plugin.SignalInfo;
import org.godotengine.godot.plugin.UsedByGodot;

import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

public class toponAD extends GodotPlugin {

    public toponAD(Godot godot) {
        super(godot);
    }

    @NonNull
    @Override
    public String getPluginName() {
        return "toponAD";
    }
    ATRewardVideoAd rewardAd = null;
    @SuppressLint("StaticFieldLeak")
    public Context context;
    @UsedByGodot
    public void init(String appid,String appkey)
    {
        context = Objects.requireNonNull(getActivity()).getApplicationContext();
        ATSDK.init(context,appid,appkey);
        ATSDK.setNetworkLogDebug(true);
        ATSDK.start();
        as_to_godot_string("sdk初始化中");
        Log.d("ToponGodot","sdk初始化中");
    }
    //激励广告
    @UsedByGodot
    public void showRewardVideoAd(String placementid)
    {
        String tag = this.getClass().toString();
        if (rewardAd == null){
            rewardAd = new ATRewardVideoAd(context,placementid);
            rewardAd.setAdListener(new ATRewardVideoListener()
            {
                @Override
                public void onRewardedVideoAdLoaded()
                {
                    as_to_godot_string("广告已加载");
                    Log.d(tag,"广告已加载");
                }

                @Override
                public void onRewardedVideoAdFailed(AdError adError)
                {
                    as_to_godot_string("广告播放失败");
                    Log.e(tag, adError.getCode());
                    Log.e(tag, adError.getFullErrorInfo());
                }

                @Override
                public void onRewardedVideoAdPlayStart(ATAdInfo atAdInfo)
                {
                    as_to_godot_string("广告播放开始");
                    Log.d(tag,"广告播放开始");
                }

                @Override
                public void onRewardedVideoAdPlayEnd(ATAdInfo atAdInfo)
                {
                    as_to_godot_string("广告播放结束");
                    Log.d(tag, "广告播放结束");
                }

                @Override
                public void onRewardedVideoAdPlayFailed(AdError adError, ATAdInfo atAdInfo)
                {
                    as_to_godot_string("广告播放失败");
                    Log.e(tag, "广告播放失败");
                    Log.e(tag, adError.getCode());
                    Log.d(tag, adError.getFullErrorInfo());
                }

                @Override
                public void onRewardedVideoAdClosed(ATAdInfo atAdInfo)
                {
                    as_to_godot_string("广告被关闭");
                    Log.d(tag, "广告被关闭");
                }

                @Override
                public void onRewardedVideoAdPlayClicked(ATAdInfo atAdInfo)
                {
                    as_to_godot_string("广告被点击");
                    Log.d(tag, "广告被点击");
                }

                @Override
                public void onReward(ATAdInfo atAdInfo)
                {
                    as_to_godot_string("奖励发放");
                    Log.d(tag, "奖励发放");
                }
            });
        }
        if (rewardAd.isAdReady())
        {
            Log.d(tag, "激励广告加载成功");
            as_to_godot_string("激励广告加载成功");
            rewardAd.show(getActivity());
        } else {
            as_to_godot_string("激励广告加载失败，正在加载中");
            Log.d(tag, "激励广告加载失败，正在加载中");
            rewardAd.load();
        }
    }


    @NonNull
    @Override
    public Set<SignalInfo> getPluginSignals() {
        Set<SignalInfo> signals = new HashSet<>();

        // 添加您的自定义信号信息
        signals.add(new SignalInfo("AD_signals", String.class));




        return signals;
    }
    public void as_to_godot_string(String a){
        emitSignal("AD_signals",a);

    }
    @UsedByGodot
    public void ceshi(String aa){
        Activity activity= getActivity();
        assert activity != null;
        activity.runOnUiThread(()->{
            Toast.makeText(activity,aa,Toast.LENGTH_LONG).show();
        });
    }
}
